define(function (require, exports, module) {

    // 通过 require 引入依赖
    // var $ = require('jquery');
    require('common');
    require('sui');
    var template = require('template');
    // require('ztree');
    // require('ztreeExhide');
    $('#form-user').validate({
        rules: {
            accountName: {
                required: true,
                minlength: 2,
                maxlength: 20
            },
            nickName: {
                required: true,
                minlength: 2,
                maxlength: 22
            },
            password: {
                required: true,
                minlength: 4,
                maxlength: 22
            }
        },
        messages: {
            accountName: "请填写",
            nickName: "请填写",
            password: "请填写"

        },
        success: function () {
            createUser();
            return false;
        }
    });

    function createUser() {

        var accountName = $.trim($('#accountName').val());
        var nickName = $.trim($('#nickName').val());
        var password = $.trim($('#password').val());
        var uremark = $.trim($('#uremark').val());
        var roleIds = new Array();
        var inputs = $('.checkbox-role');
        $.each(inputs, function (i, item) {
            if ($(item).hasClass('checked')) {
                var role = $(item).find('input').eq(0).val();
                roleIds.push(role);
            }
        });

        var data = {
            accountName: accountName,
            nickName: nickName,
            uremark: uremark,
            password: password,
            roleIds: JSON.stringify(roleIds)
        };
        $('.create-user').prop('disabled', 'disabled');
        $.post('/sysUser/add', data, function (o) {
            switch (o.code) {
                case 0:
                    toastSuccess('新增用户成功');
                    setTimeout(function () {
                        window.location.href = '/sysUser'
                    }, 2000);
                    break;
                default:
                    toastError(o.msg);
                    $('.create-user').removeAttr('disabled');
            }
        })


    }


});
